// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import TurnPage, {EDirection} from "./TurnPage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property({type:[cc.Node]})
    pageList:Array<cc.Node> = [];

    @property({type:TurnPage})
    pageCtrl:TurnPage = null;

    private _curPageIdx:number = 0;

    private get nextPageIdx():number {
        let len = this.pageList.length ;
        if(len < 0) 
            return;

        let val = (this._curPageIdx + 1) % len;
        return val;
    }

    private get prevPageIdx():number {
        let len = this.pageList.length ;
        if(len < 0) 
            return;

        let val = Math.abs(this._curPageIdx - 1) % len;
        return val;
    }


    start() {
        if(this.pageList.length < 0) 
            return;
        this._curPageIdx = 0;

        if(this.pageCtrl) {
            this.pageCtrl.init(this.pageList[this._curPageIdx]);
        }
    }


    onBtnLeft() {
        if(this.pageList.length < 0) 
            return;

        if(this.pageCtrl) {
            console.log("Turn left");
            this._curPageIdx = this.prevPageIdx;
            this.pageCtrl.turnBeg(EDirection.Backward, this.pageList[this._curPageIdx]);
        }
    }

    onBtnRight() {
        if(this.pageList.length < 0) 
            return;


        if(this.pageCtrl) {
            console.log("Turn right");
            this._curPageIdx = this.nextPageIdx;
            this.pageCtrl.turnBeg(EDirection.Forward, this.pageList[this._curPageIdx]);
        }
    }
}
